home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / engine.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  5KB  |  197 lines

  1. /* --------------------------------- engine.c ------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Calculate engine thrust.
  8.  * Based on Stevens&Lewis AC&S, F16 model.
  9.  *
  10.  * To handle large numbers we divide altitude by 10. We can now handle alt over
  11.  * the 65000 'short' limit. The 'thrust' returned is also divided by 10.
  12. */
  13.  
  14. #include "plane.h"
  15.  
  16.  
  17. /* Return command power [0..10000] for a throttle setting [0..100].
  18.  * At 75% setting we get 50% power command.
  19. */
  20. LOCAL_FUNC short NEAR    /* x100 */
  21. tgear (short thtl)    /* x100 */
  22. {
  23.     if (thtl <= 75)
  24.         return (67 * thtl);
  25.     else
  26.         return (200 * thtl - 100*100);
  27. }
  28.  
  29. /* Return reciprocal time constant [0..1000].
  30. */
  31. LOCAL_FUNC short NEAR
  32. rtau (short dp)        /* x100 */
  33. {
  34.     if (dp <= 25*100)
  35.         return (1000);
  36.     else if (dp >= 50*100)
  37.         return (100);
  38.     else
  39.         return (1900 - muldiv (36, dp, 100));
  40. }
  41.  
  42. /* Return rate of change of power [0..10000].
  43. */
  44. LOCAL_FUNC short NEAR    /* x 100 */
  45. pdot (short pow, short cpow)    /* x100, x100 */
  46. {
  47.     short    t;    /* x1000 */
  48.     short    p2;    /* x100 */
  49.  
  50.     if (cpow >= 50*100) {
  51.         if (pow >= 50*100) {
  52.             t = 5*1000;
  53.             p2 = cpow;
  54.         } else {
  55.             p2 = 60*100;
  56.             t = rtau (p2-pow);
  57.         }
  58.     } else {
  59.         if (pow >= 50*100) {
  60.             t = 5*1000;
  61.             p2 = 40*100;
  62.         } else {
  63.             p2 = cpow;
  64.             t = rtau (p2-pow);
  65.         }
  66.     }
  67.     return (muldiv (t, p2-pow, 1000));
  68. }
  69.  
  70. /*    sl    10k    20k    30k    40k    50k */
  71.  
  72. static short a[6][6] = {                /* idle    */
  73.     {1060,    670,    880,    1140,    1500,    1860},    /* M0.0    */
  74.     {635,    425,    690,    1010,    1330,    1700},    /* M0.2    */
  75.     {60,    25,    345,    755,    1130,    1525},    /* M0.4    */
  76.     {-1020,    -710,    -300,    350,    910,    1360},    /* M0.6    */
  77.     {-2700,    -1900,    -1300,    -247,    600,    1100},    /* M0.8    */
  78.     {-3600,    -1400,    -595,    -342,    -200,    700}    /* M1.0    */
  79. };
  80.  
  81. static short b[6][6] = {                /* mil    */
  82.     {12680,    9150,    6200,    3950,    2450,    1400},
  83.     {12680,    9150,    6313,    4040,    2470,    1400},
  84.     {12610,    9312,    6610,    4290,    2600,    1560},
  85.     {12640,    9839,    7090,    4660,    2840,    1660},
  86.     {12390,    10176,    7750,    5320,    3250,    1930},
  87.     {11680,    9848,    8050,    6100,    3800,    2310}
  88. };
  89.  
  90. static short c[6][6] = {                /* ab    */
  91.     {20000,    15000,    10800,    7000,    4000,    2500},
  92.     {21420,    15700,    11225,    7323,    4435,    2600},
  93.     {22700,    16860,    12250,    8154,    5000,    2835},
  94.     {24240,    18910,    13760,    9285,    5700,    3215},
  95.     {26070,    21075,    15975,    11115,    6860,    3950},
  96.     {28886,    23319,    18300,    13484,    8642,    5057}
  97. };    
  98.  
  99. LOCAL_FUNC int NEAR
  100. thrust (short pow, Ushort alt, short amach)
  101. {
  102.     int    i, dh, m, dm, cdh, s, t, tmil, tidl, tmax;
  103.  
  104.     i  = alt / 1000;
  105.     if (i > 4)
  106.         i = 4;
  107.     dh = alt - 1000*i;
  108.     cdh = 1000 - dh;
  109.  
  110.     m  = amach*5 / 100;
  111.     if (m > 4)
  112.         m = 4;
  113.     dm = amach*5 - 100*m;
  114.  
  115.     s = muldiv (b[m][i],   cdh, 1000) + muldiv (b[m][i+1],   dh, 1000);
  116.     t = muldiv (b[m+1][i], cdh, 1000) + muldiv (b[m+1][i+1], dh, 1000);
  117.     tmil = s + muldiv (t-s, dm, 100);
  118.     if (pow < 50*100) {
  119.         s = muldiv (a[m][i], cdh, 1000)
  120.             + muldiv (a[m][i+1], dh, 1000);
  121.         t = muldiv (a[m+1][i], cdh, 1000)
  122.             + muldiv (a[m+1][i+1], dh, 1000);
  123.         tidl = s + muldiv(t-s, dm , 100);
  124.         t = tidl + muldiv (tmil-tidl, pow, 50*100);
  125.     } else {
  126.         s = muldiv (c[m][i], cdh, 1000)
  127.             + muldiv (c[m][i+1], dh, 1000);
  128.         t = muldiv (c[m+1][i], cdh, 1000)
  129.             + muldiv (c[m+1][i+1], dh, 1000);
  130.         tmax = s + muldiv (t-s, dm, 100);
  131.         t = tmil + muldiv (tmax-tmil, pow-50*100, 50*100);
  132.     }
  133.     return (t/10);
  134. }
  135.  
  136. extern void FAR
  137. f16engine (OBJECT *p, short sos)
  138. {
  139.     short    thtl, neg, amach, t;
  140.     long    alt;
  141.  
  142.     neg = FONE;
  143.     if (100 == EX->throttle)
  144.         thtl = 75 + muldiv (EX->afterburner, 100-75, 100);
  145.     else {
  146.         thtl = muldiv (EX->throttle, 75, 100);
  147.         if (thtl < 0) {
  148.             thtl = -thtl;
  149.             neg = -FONE/2;    /* 50% efficient reverse thrust? */
  150.         }
  151.     }
  152.  
  153.     alt = p->R[Z] * 328L / (100L * VONE);
  154.     if (alt > 320000L)
  155.         alt = 320000L;
  156.  
  157.     if (EX->flags & PF_FLAMEOUT) {
  158.         if (EX->fuel > 0 && alt*4 < EP->ceiling*3)
  159.             EX->flags &= ~PF_FLAMEOUT;    /* re ingnite */
  160.     } else if (alt > EP->ceiling || EX->fuel <= 0)
  161.         EX->flags |= PF_FLAMEOUT;        /* flameout */
  162.  
  163.     if (EX->flags & PF_FLAMEOUT)
  164.         t = 0;
  165.     else
  166.         t = tgear (thtl);
  167.     t = pdot (EX->power, t);
  168.     EX->power += TADJ (t);
  169.     if (EX->power > 10000)
  170.         EX->power = 10000;
  171.     else if (EX->power < 0)
  172.         EX->power = 0;
  173.  
  174.     alt /= 10;
  175.     amach = muldiv (p->speed, 100, sos);
  176.     t = fmul (neg, thrust (EX->power, (Ushort)alt, amach));
  177.     EX->thrust = muldiv (t, EP->ab_thrust, 2383);
  178.     if ((EX->flags & PF_FLAMEOUT) && 0 == EX->power && EX->thrust > 0)
  179.         EX->thrust = 0;
  180.  
  181.     if (EX->fuel > 0) {
  182.  
  183. /* sfc diminishes to 0.8 (of its sea-level value) at 36000 feet then stays
  184.  * stable.
  185. */
  186.         t = EX->afterburner ? EP->ab_sfc : EP->mil_sfc;
  187.         if (alt < 3600L)
  188.             t -= fmul (t, muldiv (FCON(0.2), (int)alt, 3600));
  189.         else
  190.             t -= fmul (t, FCON(0.2));
  191.         EX->fuelRate = muldiv (iabs (EX->thrust), t, 60*60/10);
  192.         EX->fuel -= TADJ(EX->fuelRate);
  193.         if (EX->fuel < 0)
  194.             EX->fuel = 0;
  195.     }
  196. }
  197.